package server;

import java.net.*;
import java.io.*;
import java.util.*;

class SocketManager extends ArrayList
{
    synchronized public void add(Socket socket)
    {
        super.add(socket);
    }

    synchronized public void remove(Socket socket)
    {
        super.remove(socket);
    }

    synchronized public void sendToAll(String str) throws IOException
    {
        PrintWriter writer = null;
        Socket socket;

        for (int i=0; i<size(); i++) {
            socket = (Socket)get(i);
            try {
                writer = new PrintWriter(socket.getOutputStream(), true);
                if (writer != null) {
                    writer.println(str + "\0");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    synchronized public void sendClientInfo() throws IOException
    {
        String info = "current total num of person:" + size();
        System.out.println(info);
        sendToAll(info);
    }
}
